package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户头像更新
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity headerUpdate(MultipartFile headPhoto) throws IOException {
        userInfoService.updateHeadPhoto(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机发送验证码
     *
     * @param
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendCodePhone() {
        userInfoService.sendCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 验证手机验证码-修改手机
     *
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkCodePhone(@RequestBody Map map) {
        Map result = userInfoService.checkCode(map);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存手机号
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map) {
        userInfoService.updatePhone(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 分页列表：
     * @param type  1 互相关注 2 我关注 3 粉丝 4 谁看过我
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable(value = "type")Integer type,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize,String nickname){
        PageResult pageResult = new PageResult();
        if(type==1){
            //查询朋友
             pageResult = userInfoService.selectFriend(page,pagesize,nickname);
        }else if (type==2){
            pageResult = userInfoService.selectIlike(page,pagesize,nickname);
        }else if (type == 3){
            pageResult = userInfoService.selectlikeme(page,pagesize,nickname);
        }else {
            pageResult = userInfoService.selectSeeme(page,pagesize,nickname);
        }
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 喜欢： 取消
     * @param id
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity makeunlike(@PathVariable("uid") Long id){
        userInfoService.makeunlike(id);
        return ResponseEntity.ok(null);
    }


    /**
     * 对粉丝进行喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity likefans(@PathVariable("uid") Long id){
        userInfoService.likefans(id);
        return ResponseEntity.ok(null);
    }


    @GetMapping("/counts")
    public ResponseEntity countlike(){
        CountVo countVo = userInfoService.getCount();
        return  ResponseEntity.ok(countVo);
    }
}
